package com.ocom.payconfig.controller;

import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfChannelInfo;
import com.ocom.common.entity.card.EntityXfMerchPayInfo;
import com.ocom.common.entity.master.EntityPayChannel;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.AddConfigureInfoRequest;
import com.ocom.common.request.payconfig.DeleteConfigureInfoRequest;
import com.ocom.common.request.payconfig.GetConfigureInfoRequest;
import com.ocom.common.request.payconfig.UpdateConfigureInfoRequest;
import com.ocom.common.request.web.OneConfigureInfoRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.pay.GzhPayVo;
import com.ocom.payconfig.service.PayChannelService;
import com.ocom.payconfig.service.XfChannelInfoService;
import com.ocom.payconfig.service.XfMerchPayInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2.19.获取消费通道配置信息（getConfigureInfo）
 * 2.20.添加消费通道配置信息（addConfigureInfo）
 * 2.21.修改消费通道配置信息（updateConfigureInfo）
 * 2.22.删除消费通道配置信息（deleteConfigureInfo）
 */
@RestController
public class XfChannelInfoController {


    @Autowired
    XfChannelInfoService xfChannelInfoService;

    @Autowired
    PayChannelService payChannelService;

    @Autowired
    XfMerchPayInfoService xfMerchPayInfoService;


    //2.19.获取消费通道配置信息（getConfigureInfo）
    @PostMapping(PayConfigUrl.Url_getConfigureInfo)
    public Result getConfigureInfo(@RequestBody GetConfigureInfoRequest request){
        List<EntityXfChannelInfo> list = xfChannelInfoService.getConfigureList(request.getComId(),request);
        Map<String,Object>  resMap =new HashMap<>();
        resMap.put("total",0);
        if(list!=null){
            resMap.put("total",list.size());
            List<EntityXfMerchPayInfo> merchPayInfos = xfMerchPayInfoService.getCompanyId(request.getComId());
            if(merchPayInfos!=null && merchPayInfos.size()>0){
                list.forEach(entityXfChannelInfo -> {
                            for (EntityXfMerchPayInfo entityXfMerchPayInfo:merchPayInfos){
                                if(entityXfMerchPayInfo.getMerchId().equals(entityXfChannelInfo.getMerchId()))
                                    entityXfChannelInfo.setCompanyId(entityXfMerchPayInfo.getCompanyId());
                            }
                        }
                );
            }
        }
        resMap.put("list",list);
        return ResultUtil.success(resMap);
    }

    //获取一条详情
    @PostMapping(PayConfigUrl.Url_ConfigureInfo)
    public Result configureInfo(@RequestBody OneConfigureInfoRequest request){
        return ResultUtil.success(xfChannelInfoService.OneConfigureInfo(request.getComId(),request));
    }

    //2.20.添加消费通道配置信息（addConfigureInfo）
    @PostMapping(PayConfigUrl.Url_addConfigureInfo)
    public Result addConfigureInfo(@RequestBody AddConfigureInfoRequest request){
        EntityPayChannel entityPayChannel = payChannelService.getPayTipByCode(request.getPayWay());
        request.setPayWay(entityPayChannel.getPayWay());
        EntityXfChannelInfo entityXfChannelInfo=xfChannelInfoService.addConfigureInfo(request.getComId(),request);
        if(entityXfChannelInfo==null){
            return ResultUtil.error(ResultEnum.Err_2016);
        }
        return ResultUtil.success(entityXfChannelInfo);
    }
    //2.21.修改消费通道配置信息（updateConfigureInfo）
    @PostMapping(PayConfigUrl.Url_updateConfigureInfo)
    public Result updateConfigureInfo(@RequestBody UpdateConfigureInfoRequest request){
        EntityPayChannel entityPayChannel = payChannelService.getPayTipByCode(request.getPayWay());
        request.setPayWay(entityPayChannel.getPayWay());
        EntityXfChannelInfo entityXfChannelInfo=xfChannelInfoService.updateConfigureInfo(request.getComId(),request);
        if(entityXfChannelInfo==null){
            return ResultUtil.error(ResultEnum.Err_3014);
        }
        return ResultUtil.success(entityXfChannelInfo);
    }
    //2.22.删除消费通道配置信息（deleteConfigureInfo）
    @PostMapping(PayConfigUrl.Url_deleteConfigureInfo)
    public Result deleteConfigureInfo(@RequestBody DeleteConfigureInfoRequest request){
        boolean b=xfChannelInfoService.deleteConfigureInfo(request.getComId(),request);
        if(!b){
            return ResultUtil.error(ResultEnum.Err_3013);
        }else {
            return ResultUtil.success();
        }
    }



    //2.22.删除消费通道配置信息（deleteConfigureInfo）
    @PostMapping(PayConfigUrl.Url_getDeleteConfigureInfo)
    public Result  getDeleteConfigureInfo(Long companyId,Integer id){
        return xfChannelInfoService.getDeleteConfigureInfo(companyId,id);
    }



    //2.56获取单位线上充值通道列表(getRechargePayWay)
    @PostMapping(PayConfigUrl.Url_getRechargePayWay)
    public Result  getRechargePayWay(Long companyId,@RequestParam(required = false,defaultValue = "0") Long merchId
            ,@RequestParam(required = false,defaultValue = "1") Integer type){
        List<GzhPayVo> list = new ArrayList<>();
        if(type==1){
            list = xfChannelInfoService.getGzhPayList(companyId,merchId,payChannelService.payList());
        }else{
            list = xfChannelInfoService.getGzhPayList2(companyId,merchId,payChannelService.payList());
        }
        if(list==null  || list.size()<=0){
            return ResultUtil.error(ResultEnum.Err_2030);
        }
        return ResultUtil.success(list);
    }



}
